Add a simple motion eventcontroller
authorMatthias Clasen <mclasen@redhat.com>
Mon, 11 Dec 2017 23:29:33 +0000 (18:29 -0500)
committerCarlos Garnacho <carlosg@gnome.org>
Mon, 16 Jul 2018 13:43:43 +0000 (15:43 +0200)
This can serve as a replacement for the legacy
event signals for enter/leave/motion notify.

gtk/Makefile.am
gtk/gtk.h
gtk/gtkeventcontrollermotion.c [new file with mode: 0644]
gtk/gtkeventcontrollermotion.h [new file with mode: 0644]

index 37336d540fc6bc190b032c78aab9b218b93f0341..ec81f395d763d973b1cbd69ccffb41dd89fd66a1 100644 (file)
@@ -192,6 +192,7 @@ gtk_public_h_sources =              \
        gtkenums.h              \
        gtkeventbox.h           \
        gtkeventcontroller.h    \
+       gtkeventcontrollermotion.h      \
        gtkeventcontrollerscroll.h      \
        gtkexpander.h           \
        gtkfilechooser.h        \
@@ -758,6 +759,7 @@ gtk_base_c_sources =                \
        gtkentrycompletion.c    \
        gtkeventbox.c           \
        gtkeventcontroller.c    \
+       gtkeventcontrollermotion.c      \
        gtkeventcontrollerscroll.c      \
        gtkexpander.c           \
        gtkfilechooser.c        \
index 1ce5e5b37ea09498ac40b1ffb12e1134f9d96283..46cfbfc063ab4a26d9798bff40ba34bc8d40eff7 100644 (file)
--- a/gtk/gtk.h
+++ b/gtk/gtk.h
@@ -94,6 +94,7 @@
 #include <gtk/gtkenums.h>
 #include <gtk/gtkeventbox.h>
 #include <gtk/gtkeventcontroller.h>
+#include <gtk/gtkeventcontrollermotion.h>
 #include <gtk/gtkeventcontrollerscroll.h>
 #include <gtk/gtkexpander.h>
 #include <gtk/gtkfixed.h>
diff --git a/gtk/gtkeventcontrollermotion.c b/gtk/gtkeventcontrollermotion.c
new file mode 100644 (file)
index 0000000..6fe5f23
--- /dev/null
@@ -0,0 +1,160 @@
+/* GTK - The GIMP Toolkit
+ * Copyright (C) 2017, Red Hat, Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * Author(s): Matthias Clasen <mclasen@redhat.com>
+ */
+
+/**
+ * SECTION:gtkeventcontrollermotion
+ * @Short_description: Event controller for motion events
+ * @Title: GtkEventControllerMotion
+ * @See_also: #GtkEventController
+ *
+ * #GtkEventControllerMotion is an event controller meant for situations
+ * where you need to track the position of the pointer.
+ *
+ * This object was added in 3.94.
+ **/
+#include "config.h"
+
+#include "gtkintl.h"
+#include "gtkwidget.h"
+#include "gtkeventcontrollerprivate.h"
+#include "gtkeventcontrollermotion.h"
+#include "gtktypebuiltins.h"
+#include "gtkmarshalers.h"
+
+struct _GtkEventControllerMotion
+{
+  GtkEventController parent_instance;
+};
+
+struct _GtkEventControllerMotionClass
+{
+  GtkEventControllerClass parent_class;
+};
+
+enum {
+  ENTER,
+  LEAVE,
+  MOTION,
+  N_SIGNALS
+};
+
+static guint signals[N_SIGNALS] = { 0 };
+
+G_DEFINE_TYPE (GtkEventControllerMotion, gtk_event_controller_motion, GTK_TYPE_EVENT_CONTROLLER)
+
+static gboolean
+gtk_event_controller_motion_handle_event (GtkEventController *controller,
+                                          const GdkEvent     *event)
+{
+  GtkEventControllerClass *parent_class;
+  GdkEventType type;
+
+  type = gdk_event_get_event_type (event);
+  if (type == GDK_ENTER_NOTIFY)
+    g_signal_emit (controller, signals[ENTER], 0);
+  else if (type == GDK_LEAVE_NOTIFY)
+    g_signal_emit (controller, signals[LEAVE], 0);
+  else if (type == GDK_MOTION_NOTIFY)
+    {
+      double x, y;
+      gdk_event_get_coords (event, &x, &y);
+      g_signal_emit (controller, signals[MOTION], 0, x, y);
+    }
+
+  parent_class = GTK_EVENT_CONTROLLER_CLASS (gtk_event_controller_motion_parent_class);
+
+  return parent_class->handle_event (controller, event);
+}
+
+static void
+gtk_event_controller_motion_class_init (GtkEventControllerMotionClass *klass)
+{
+  GtkEventControllerClass *controller_class = GTK_EVENT_CONTROLLER_CLASS (klass);
+
+  controller_class->handle_event = gtk_event_controller_motion_handle_event;
+
+  /**
+   * GtkEventControllerMotion::enter:
+   * @controller: The object that received the signal
+   *
+   * Signals that the pointer has entered the widget.
+   */
+  signals[ENTER] =
+    g_signal_new (I_("enter"),
+                  GTK_TYPE_EVENT_CONTROLLER_MOTION,
+                  G_SIGNAL_RUN_FIRST,
+                  0, NULL, NULL,
+                  NULL,
+                  G_TYPE_NONE, 0);
+  /**
+   * GtkEventControllerMotion::leave:
+   * @controller: The object that received the signal
+   *
+   * Signals that pointer has left the widget.
+   */
+  signals[LEAVE] =
+    g_signal_new (I_("leave"),
+                  GTK_TYPE_EVENT_CONTROLLER_MOTION,
+                  G_SIGNAL_RUN_FIRST,
+                  0, NULL, NULL,
+                 NULL,
+                  G_TYPE_NONE, 0);
+
+  /**
+   * GtkEventControllerMotion::motion:
+   * @controller: The object that received the signal
+   * @x: the x coordinate
+   * @y: the y coordinate
+   *
+   * Emitted when the pointer moves inside the widget.
+   */
+  signals[MOTION] =
+    g_signal_new (I_("motion"),
+                  GTK_TYPE_EVENT_CONTROLLER_MOTION,
+                  G_SIGNAL_RUN_FIRST,
+                  0, NULL, NULL,
+                  NULL,
+                  G_TYPE_NONE, 2, G_TYPE_DOUBLE, G_TYPE_DOUBLE);
+}
+
+static void
+gtk_event_controller_motion_init (GtkEventControllerMotion *motion)
+{
+}
+
+/**
+ * gtk_event_controller_motion_new:
+ * @widget: a #GtkWidget
+ *
+ * Creates a new event controller that will handle motion events
+ * for the given @widget.
+ *
+ * Returns: a new #GtkEventControllerMotion
+ *
+ * Since: 3.94
+ **/
+GtkEventController *
+gtk_event_controller_motion_new (GtkWidget *widget)
+{
+  g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
+
+  return g_object_new (GTK_TYPE_EVENT_CONTROLLER_MOTION,
+                       "widget", widget,
+                       NULL);
+}
diff --git a/gtk/gtkeventcontrollermotion.h b/gtk/gtkeventcontrollermotion.h
new file mode 100644 (file)
index 0000000..42b6aac
--- /dev/null
@@ -0,0 +1,50 @@
+/* GTK - The GIMP Toolkit
+ * Copyright (C) 2017, Red Hat, Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * Author(s): Matthias Clasen <mclasen@redhat.com>
+ */
+
+#ifndef __GTK_EVENT_CONTROLLER_MOTION_H__
+#define __GTK_EVENT_CONTROLLER_MOTION_H__
+
+#if !defined (__GTK_H_INSIDE__) && !defined (GTK_COMPILATION)
+#error "Only <gtk/gtk.h> can be included directly."
+#endif
+
+#include <gdk/gdk.h>
+#include <gtk/gtkeventcontroller.h>
+
+G_BEGIN_DECLS
+
+#define GTK_TYPE_EVENT_CONTROLLER_MOTION         (gtk_event_controller_motion_get_type ())
+#define GTK_EVENT_CONTROLLER_MOTION(o)           (G_TYPE_CHECK_INSTANCE_CAST ((o), GTK_TYPE_EVENT_CONTROLLER_MOTION, GtkEventControllerMotion))
+#define GTK_EVENT_CONTROLLER_MOTION_CLASS(k)     (G_TYPE_CHECK_CLASS_CAST ((k), GTK_TYPE_EVENT_CONTROLLER_MOTION, GtkEventControllerMotionClass))
+#define GTK_IS_EVENT_CONTROLLER_MOTION(o)        (G_TYPE_CHECK_INSTANCE_TYPE ((o), GTK_TYPE_EVENT_CONTROLLER_MOTION))
+#define GTK_IS_EVENT_CONTROLLER_MOTION_CLASS(k)  (G_TYPE_CHECK_CLASS_TYPE ((k), GTK_TYPE_EVENT_CONTROLLER_MOTION))
+#define GTK_EVENT_CONTROLLER_MOTION_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), GTK_TYPE_EVENT_CONTROLLER_MOTION, GtkEventControllerMotionClass))
+
+typedef struct _GtkEventControllerMotion GtkEventControllerMotion;
+typedef struct _GtkEventControllerMotionClass GtkEventControllerMotionClass;
+
+GDK_AVAILABLE_IN_3_24
+GType               gtk_event_controller_motion_get_type (void) G_GNUC_CONST;
+
+GDK_AVAILABLE_IN_3_24
+GtkEventController *gtk_event_controller_motion_new      (GtkWidget *widget);
+
+G_END_DECLS
+
+#endif /* __GTK_EVENT_CONTROLLER_MOTION_H__ */